package com.example.fish.service.impl;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.fish.mapper.IllegalMapper;
import com.example.fish.mapper.SecretMapper;
import com.example.fish.mapper.UserInfoMapper;
import com.example.fish.pojo.Illegal;
import com.example.fish.pojo.Secret;
import com.example.fish.pojo.SecretExample;
import com.example.fish.pojo.UserInfo;
import com.example.fish.service.SecretService;
import com.example.fish.service.ex.SecretInsertException;
import com.example.fish.service.ex.SecretSearchException;
import com.example.fish.utils.SensitiveWordFilter;
import com.example.fish.vo.SecretList;
import com.example.fish.vo.TitleSecret;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class SecretServiceImpl implements SecretService {

    @Autowired
    private SecretMapper secretMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Resource
    private SensitiveWordFilter sensitiveWordFilter;
    @Resource
    private IllegalMapper illegalMapper;
    /***
     * 用于添加秘籍的实现
     * @param userid 当前用户的id
     * @param title 秘籍标题
     * @param type 秘籍类型
     * @param content 秘籍正文
     */
    @Override
    public void publishSecret(Integer userid, String title, String type, String content) {
        Secret secret = new Secret();
        secret.setUserid(userid);
//        敏感字判断
        String s = sensitiveWordFilter.filterSensitiveWord(content);
        secret.setContent(s);
//        违规行为+1
        boolean b = sensitiveWordFilter.containsSensitiveWord(content);
        if (b){
            Illegal illegal = new Illegal();
            illegal.setUserid(userid);
            illegalMapper.insert(illegal);
        }
        secret.setTitle(title);
//        发布时间
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将日期格式化为字符串
        String formattedDate = currentDate.format(formatter);
        secret.setPublishtime(formattedDate);
        secret.setType(type);

        int insert = secretMapper.insert(secret);
        if (insert == 0){
            throw new SecretInsertException("秘籍保存失败");
        }

    }

    /***
     * 用来分页查询所有的SecretList
     * @return 分页信息
     */
    @Override
    public PageInfo<SecretList> getAllSecretByPage(Integer pageNum) {
        Integer pageSize = 5;
        SecretExample example = new SecretExample();
        example.setOrderByClause("publishtime  DESC");
        example.createCriteria();
        int count = secretMapper.countByExample(example);
        PageHelper.startPage(pageNum, pageSize);
        List<Secret> secrets = secretMapper.selectByExample(example);
        List<SecretList> list = new ArrayList<>();
        if (secrets == null){
            throw new SecretSearchException("秘籍查询发生错误");
        }
        for (Secret secret : secrets) {
//        根据secret中的userid查询对应头像和姓名
            UserInfo user = userInfoMapper.selectById(secret.getUserid());
//            封装
            SecretList secretList = new SecretList();
            secretList.setSecret(secret);
            secretList.setUserid(user.getUserid());
            secretList.setAvatar(user.getAvator());
            secretList.setUsername(user.getName());
            secretList.setFishage(user.getFishage());
            list.add(secretList);
        }
        PageInfo<SecretList> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count);
        return pageInfo;
    }

    /***
     * search模块
     * @param keyword 搜索关键字
     * @return 分页数据
     */
    @Override
    public PageInfo<SecretList> getSecretByKeyword(Integer pageNum,String keyword) {
        Integer pageSize = 5 ;
        SecretExample example = new SecretExample();
        example.setOrderByClause("publishtime  DESC");
        if (StringUtils.isNotBlank(keyword)){
            example.createCriteria().andContentLike("%"+keyword+"%");
            example.or(example.createCriteria().andTitleLike("%"+keyword+"%"));
            example.or(example.createCriteria().andTypeLike("%"+keyword+"%"));
        }
        int i = secretMapper.countByExample(example);
        PageHelper.startPage(pageNum, pageSize);
        List<Secret> secrets = secretMapper.selectByExample(example);
        List<SecretList> list = new ArrayList<>();
        for (Secret secret : secrets) {
//            根据secret中的userid查询对应头像和姓名
            UserInfo user = userInfoMapper.selectById(secret.getUserid());
//            封装
            SecretList secretList = new SecretList();
            secretList.setSecret(secret);
            secretList.setUserid(user.getUserid());
            secretList.setAvatar(user.getAvator());
            secretList.setUsername(user.getName());
            secretList.setFishage(user.getFishage());
            list.add(secretList);
        }
        PageInfo<SecretList> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo);
        pageInfo.setTotal(i);
        return pageInfo;
    }

    @Override
    public void setAgree(Integer secretid) {
        Secret secret = secretMapper.selectByPrimaryKey(secretid);
        secret.setAgree(secret.getAgree()+1);
        secretMapper.updateByPrimaryKey(secret);
    }

    @Override
    public void cancelAgree(Integer secretid) {
        Secret secret = secretMapper.selectByPrimaryKey(secretid);
        secret.setAgree(secret.getAgree() - 1);
        secretMapper.updateByPrimaryKey(secret);
    }

    @Override
    public void setDisagree(Integer secretid) {
        Secret secret = secretMapper.selectByPrimaryKey(secretid);
        secret.setDisagree(secret.getDisagree()+1);
        secretMapper.updateByPrimaryKey(secret);
    }

    @Override
    public void cancelDisagree(Integer secretid) {
        Secret secret = secretMapper.selectByPrimaryKey(secretid);
        secret.setDisagree(secret.getDisagree() - 1);
        secretMapper.updateByPrimaryKey(secret);
    }

    /***
     * 根据id查询秘籍
     * @param secretid id
     * @return secret list
     */
    @Override
    public SecretList getSecretBySecretid(Integer secretid) {
        Secret secret = secretMapper.selectByPrimaryKey(secretid);
        SecretList secretList = new SecretList();
        secretList.setSecret(secret);
        UserInfo userInfo = userInfoMapper.selectById(secret.getUserid());
        secretList.setUserid(userInfo.getUserid());
        secretList.setAvatar(userInfo.getAvator());
        secretList.setUsername(userInfo.getName());
        secretList.setFishage(userInfo.getFishage());
        return secretList;
    }

    @Override
    public List<TitleSecret> titleList() {
        SecretExample example = new SecretExample();
        example.setOrderByClause("agree DESC");
        List<Secret> secrets = secretMapper.selectByExample(example);
        List<Secret> secrets1 = secrets.subList(0, 6);
        List<TitleSecret> list = new ArrayList<>();
        for (Secret secret : secrets1) {
            TitleSecret titleSecret = new TitleSecret();
            titleSecret.setTitle(secret.getTitle());
            titleSecret.setAgree(secret.getAgree());
            titleSecret.setSecretid(secret.getSecretid());
            list.add(titleSecret);
        }
        return list;
    }
}
